<?php

namespace App\Listeners;

use App\Events\OrderCompletedEvent;
use App\Extend\IPweb;
use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

class GetFlowTotalFull implements ShouldQueue
{
    /**
     * 任务被处理的延迟时间（秒）。
     *
     * @var int
     */
    public $delay = 60;


    public $ipweb;
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct(IPweb $IPweb)
    {
        $this->ipweb = $IPweb;
    }

    /**
     * Handle the event.
     *
     * @param  \App\Events\OrderCompletedEvent  $event
     * @return void
     */
    public function handle(OrderCompletedEvent $event)
    {
        $orderInfo = $event->orderInfo;
        if ($orderInfo->proxy_type !== 1) {
            return true;
        }
        Log::info('触发GetFlowTotalFull监听器 -> 订单ID：' . $orderInfo->order_nu);

        if ($orderInfo->proxy_type != 1) {
            $user = User::find($orderInfo->user_id);
            $user->has_static = 1;
            $user->save();
            return;
        }
        $user = User::find($orderInfo->user_id);
        $user->has_dynamic = 1;
        //添加子账号
        $result = $this->ipweb->getFlowTotalFull($user->auth_token);
        if ($result && $result['code'] == 200 && !empty($result['data'])) {
            $data = $result['data']['account'];
            $user->traffic = $data['residualFlow'];
            $user->total_traffic = $data['flowCount'];
            $user->consume_traffic = $data['consumeFlow'];
        }
        $user->save();

        Log::info('GetFlowTotalFull监听器执行完毕 --> 订单ID：' . $orderInfo->order_nu);
    }
}
